import { defineStore } from 'pinia'
import { Local } from '../utils/storage'
//import createAxios from '../utils/axios'
//import { reactive } from 'vue'

//export const useConfig = defineStore

interface TreeNode {
  id: number;
  name: string;
  title: string;
  children?: TreeNode[];
}

export const useConfig = defineStore('config', {
    state: () => {
        return {
          site: {
            site_name: 'ddd',
          },
          propose: [],
          tree: [] as TreeNode[],
        }
    },
    actions: {
          // 设置栏目
          setPropose (propose: any) {
            this.propose = propose
            // 缓存
            Local.set('config', this.$state)
          },

          // 设置推荐栏目
          setTree (tree: any) {
            this.tree = tree
            // 缓存
            Local.set('config', this.$state)
          },

           // 设置推荐栏目
          setSite (site: any) {
            this.site = site
            // 缓存
            Local.set('config', this.$state)
          },

          // 设置栏目
          getPropose () {
            return this.propose
          },

          // 设置推荐栏目
          getTree () {
            return this.tree
          },

           // 设置推荐栏目
          getSite () {
            return this.site
          },

          // 初始化
          /*init () {
            if(!this.site){
              createAxios({
                url: '/frontend/home/init',
                method: 'post',
              })
                .then((res) => {
                  const config = useConfig()
                  config.setPropose(res.data.propose)
                  config.setTree(res.data.tree)
                  config.setSite(res.data.config)
                  console.log('初始化配置请求结果', res)
                })
                .catch(() => {

                })
            }
          }*/

          // 站点名称
          getSiteName (): string {
              return this.site.site_name
          }
    }
})
